@extends('layouts.admin')

@section('admin-content')
    <div class="box">
        @include('flash::message')
        <div class="box-header">
            <form action="{{ url("project/search") }}" method="POST">
                <div class="col-xs-12 col-sm-8" style="padding-left: 0;margin-bottom: 10px;">
                    <div class="input-group">
                        <input type="text" name="name" class="form-control search-query"
                               placeholder="{{ trans("labels.project.name") }}" value="{{ $search }}">
                        <span class="input-group-btn">
                            {{ csrf_field() }}
                            <button type="submit" class="btn btn-default btn-sm">
                                {{ trans("crud.search") }} <i class="glyphicon glyphicon-search"></i>
                            </button>
                        </span>
                    </div>
                </div>
            </form>
            <a class="btn btn-default btn-sm" href="{{url('project/create')}}">
                <i class="glyphicon glyphicon-pencil"></i>
                {{ trans("crud.create") }}
            </a>
        </div>
        <div class="box-body clearfix">
            <table class="table table-striped table-bordered table-hover">
                <tr>
                    <th>{{ trans("labels.project.name") }}</th>
                    <th>{{ trans("labels.project.level") }}</th>
                    <th>{{ trans("labels.project.status") }}</th>
                    <th>{{ trans("labels.action") }}</th>
                </tr>
                @foreach($paginator as $v)
                    <tr>
                        <td>{{ $v->name }}</td>
                        <td>{{ trans("strings.project.level.".$v->level) }}</td>
                        <td>{{ trans("strings.project.status.".$v->status) }}</td>
                        <td class="text-success">
                            <div class="action-buttons">
                                <a href="javascript:void(0)" data-name="{{ $v->name }}" data-id="{{ $v->id }}"
                                   class="btn-detect">
                                    <i class="glyphicon glyphicon-screenshot"></i>
                                    {{ trans("crud.detect") }}
                                </a>
                                <a href="{{url("project/".$v->id."/edit")}}">
                                    <i class="glyphicon glyphicon-pencil"></i>
                                    {{ trans("crud.edit") }}
                                </a>
                                <a class="text-danger btn-delete" href="javascript:void(0)">
                                    <i class="glyphicon glyphicon-trash"></i>
                                    {{ trans("crud.destory") }}
                                    <form action="{{url('project/'.$v->id)}}" method="POST" class="delete_project"
                                          style="display:none">
                                        <input type="hidden" name="_method" value="delete">
                                        {{ csrf_field() }}
                                    </form>
                                </a>
                            </div>
                        </td>
                    </tr>
                @endforeach
            </table>
            {!! $paginator->render() !!}
        </div>
    </div>
@endsection

@section("admin-js")
    <script src="{{ asset('js/bootbox.js') }}"></script>
    <script type="text/javascript">
        $(function () {
            $('a.btn-delete').click(function () {
                var aobj = this;
                bootbox.confirm("{{ trans("alerts.deleteTitle") }}", function (result) {
                    if (result) {
                        $(aobj).find(".delete_project").submit();
                    }
                });
            });

            $('a.btn-detect').click(function () {
                var project_name = $(this).attr("data-name");
                var pid = $(this).attr("data-id");
                var dialog = bootbox.dialog({
                    title: project_name + '项目配置检测',
                    message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>'
                });

                $.ajax({
                    url: "{{url("project/detect")}}",
                    data: {pid: pid, _token: "{{ csrf_token() }}"},
                    dataType: "json",
                    type: "POST",
                    success: function (data) {
                        // 检测失败
                        if (data.code) {
                            dialog.find('.bootbox-body').html('<div class="alert alert-danger">'+data.log+'</div>');
                        } else {
                            dialog.find('.bootbox-body').html('<div class="alert alert-success">'+data.log+'</div>');
                        }
                    }
                });
            });
        })
    </script>
@endsection
